# Function for Aggregating Behavioral Proclivities 

get_dummy_el <- function(new_var, old_vars, pos_level, neg_level) {
  for(i in 1:length(old_vars)) {
    # uses <<- to perform assignment in global environment:
    el[,new_var][el[,old_vars[i]] == pos_level] <<- 1
    el[,new_var][el[,old_vars[i]] == neg_level] <<- 0
  }
  return(data)
}


# Conative Attitudes ------------------------------------------------------

# Absenteeism -------------------------------------------------------------

abs_vars <- c("lc1",
              "pta",
              "bring_up",
              "assemble")

el[,abs_vars] <- NA

elcb$fire_teach_q10_1_1$answer[2,]
elcb$fire_teach_q10_1_2$answer[2,]
elcb$fire_teach_q10_1_3$answer[2,]
elcb$fire_teach_q10_1_4$answer[2,]
elcb$pta_q10_2_1$answer[1,]
elcb$pta_q10_2_2$answer[1,]
elcb$pta_q10_2_3$answer[1,]
elcb$pta_q10_2_4$answer[1,]
elcb$bring_up_q10_3_1$answer[2,]
elcb$bring_up_q10_3_2$answer[2,]
elcb$bring_up_q10_3_3$answer[2,]
elcb$bring_up_q10_3_4$answer[2,]
elcb$assemble_q10_4_1$answer[1,]
elcb$assemble_q10_4_2$answer[1,]
elcb$assemble_q10_4_3$answer[1,]
elcb$assemble_q10_4_4$answer[1,]

mapply(FUN = get_dummy_el,
  new_var = abs_vars,
  old_vars = list(c("fire_teach_q10_1_1",
                    "fire_teach_q10_1_2",
                    "fire_teach_q10_1_3",
                    "fire_teach_q10_1_4"),
                  c("pta_q10_2_1",
                    "pta_q10_2_2",
                    "pta_q10_2_3",
                    "pta_q10_2_4"),
                  c("bring_up_q10_3_1",
                    "bring_up_q10_3_2",
                    "bring_up_q10_3_3",
                    "bring_up_q10_3_4"),
                  c("assemble_q10_4_1",
                    "assemble_q10_4_2",
                    "assemble_q10_4_3",
                    "assemble_q10_4_4")),
  pos_level = c(2,1,2,1),
  neg_level = c(1,2,1,2))

rm(abs_vars)

# Additional variables for repsondents who are LC1s

lc1s <- c("fire_teach_q10_1_1_2",
          "fire_teach_q10_1_2_2",
          "fire_teach_q10_1_3_2",
          "fire_teach_q10_1_4_2")

elcb$fire_teach_q10_1_1_2$answer[2,]
elcb$fire_teach_q10_1_2_2$answer[2,]
elcb$fire_teach_q10_1_3_2$answer[2,]
elcb$fire_teach_q10_1_4_2$answer[2,]

for(variable in lc1s) {
  el$lc1[el[,variable] == 2] <- 1
  el$lc1[el[,variable] == 1] <- 0
}

rm(lc1s)

# IPV ---------------------------------------------------------------------

# The reporting action  is 1 for the first q,
# 2 for the second, 1 for the third, and 2 for the fourth
elcb$action_q8_1a_1$answer[1, ]
elcb$action_q8_1a_2$answer[1, ]
elcb$action_q8_1a_3$answer[1, ]
elcb$action_q8_1a_4$answer[1, ]
elcb$action_q8_1b_1$answer[2, ]
elcb$action_q8_1b_2$answer[2, ]
elcb$action_q8_1b_3$answer[2, ]
elcb$action_q8_1b_4$answer[2, ]
elcb$action_q8_1c_1$answer[1, ]
elcb$action_q8_1c_2$answer[1, ]
elcb$action_q8_1c_3$answer[1, ]
elcb$action_q8_1c_4$answer[1, ]
elcb$action_q8_1d_1$answer[2, ]
elcb$action_q8_1d_2$answer[2, ]
elcb$action_q8_1d_3$answer[2, ]
elcb$action_q8_1d_4$answer[2, ]

# Create blank variables
ipv_vars <- c("report_police",
							"involve_lc1",
							"involve_parents",
							"involve_nabakyala")
el[, ipv_vars] <- NA

# mapply function through parameters
mapply(
	FUN = get_dummy_el,
	new_var = ipv_vars,
	old_vars = list(
		c(
			"action_q8_1a_1",
			"action_q8_1a_2",
			"action_q8_1a_3",
			"action_q8_1a_4"
		),
		c(
			"action_q8_1b_1",
			"action_q8_1b_2",
			"action_q8_1b_3",
			"action_q8_1b_4"
		),
		c(
			"action_q8_1c_1",
			"action_q8_1c_2",
			"action_q8_1c_3",
			"action_q8_1c_4"
		),
		c(
			"action_q8_1d_1",
			"action_q8_1d_2",
			"action_q8_1d_3",
			"action_q8_1d_4"
		)
	),
	pos_level = c(1, 2, 1, 2),
	neg_level = c(2, 1, 2, 1)
)


rm(ipv_vars)

# Additional variables for repsondents who are lc1s
lc1s <- c("action_q8_1b_2_1",
					"action_q8_1b_2_2",
					"action_q8_1b_2_3",
					"action_q8_1b_2_4")

# Answer 2 is in line with reporting for LC1s
elcb$action_q8_1b_2_1$answer[2, ]
elcb$action_q8_1b_2_2$answer[2, ]
elcb$action_q8_1b_2_3$answer[2, ]
elcb$action_q8_1b_2_4$answer[2, ]

for (variable in lc1s) {
	el$involve_lc1[el[, variable] == 2] <- 1
	el$involve_lc1[el[, variable] == 1] <- 0
}

rm(lc1s)

# Additional variables for repsondents who are nabakyalas
nabas <- c("action_q8_1d_2_1",
					 "action_q8_1d_2_2",
					 "action_q8_1d_2_3",
					 "action_q8_1d_2_4")

# Answer 2 is in line with reporting for Nabakyalas
elcb$action_q8_1d_2_1$answer[2, ]
elcb$action_q8_1d_2_2$answer[2, ]
elcb$action_q8_1d_2_3$answer[2, ]
elcb$action_q8_1d_2_4$answer[2, ]

for (variable in nabas) {
	el$involve_nabakyala[el[, variable] == 2] <- 1
	el$involve_nabakyala[el[, variable] == 1] <- 0
}

rm(nabas)


# Abortion ----------------------------------------------------------------


abo_vars <- c("vht",
							"see_doctor",
							"advise_councelling",
							"compassion")

el[,abo_vars] <- NA

elcb$niece_q12_4a_1$answer[2,]
elcb$niece_q12_4a_2$answer[2,]
elcb$niece_q12_4b_1$answer[1,]
elcb$niece_q12_4b_2$answer[1,]
elcb$eight_children_q12_5a_1$answer[2,]
elcb$eight_children_q12_5a_2$answer[2,]
elcb$eight_children_q12_5b_1$answer[1,]
elcb$eight_children_q12_5b_2$answer[1,]

mapply(
	FUN = get_dummy_el,
	new_var = abo_vars,
	old_vars = list(c("niece_q12_4a_1",
										"niece_q12_4a_2"),
									c("niece_q12_4b_1",
										"niece_q12_4b_2"),
									c("eight_children_q12_5a_1",
										"eight_children_q12_5a_2"),
									c("eight_children_q12_5b_1",
										"eight_children_q12_5b_2")),
	pos_level = c(2,1,2,1),
	neg_level = c(1,2,1,2))

rm(abo_vars)

# Additional variables for repsondents who are VHTs

elcb$niece_q12_4a_1_2$answer[2,]
elcb$niece_q12_4a_2_2$answer[2,]

vhts <- c("niece_q12_4a_1_2",
					"niece_q12_4a_2_2")

for(variable in vhts) {
	el$vht[el[,variable] == 2] <- 1
	el$vht[el[,variable] == 1] <- 0
}

rm(vhts)

# Additional variables for repsondents who are nabakyalas

elcb$eight_children_q12_5a_1_2$answer[2,]
elcb$eight_children_q12_5a_2_2$answer[2,]

nabas <- c("eight_children_q12_5a_1_2",
					 "eight_children_q12_5a_2_2")

for(i in 1:length(nabas)) {
	el$advise_councelling[el[,variable] == 2] <- 1
	el$advise_councelling[el[,variable] == 1] <- 0
}

rm(nabas)


# candidate policy preferences --------------------------
elcb$candidate_1
elcb$candidate_2
el$candidate <- NA 
el$candidate[el$first_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_1 == 1] <- 1
el$candidate[el$first_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_1 == 2] <- 0
el$candidate[el$second_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_1 == 2] <- 1
el$candidate[el$second_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_1 == 1] <- 0
el$candidate[el$third_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_2 == 1] <- 1
el$candidate[el$third_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_2 == 2] <- 0
el$candidate[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_2 == 2] <- 1
el$candidate[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "
						 & el$candidate_2 == 1] <- 0

# Adding the covariates for the candidates' name and gender, and the allocation of policy choices
#Add covariate for the name, gender of the candidate with the education policy:
el$name_educ <- NA
el$name_educ[el$first_policy == " lobby higher levels of government to provide more money for education in the area "] <- el$name_1[el$first_policy == " lobby higher levels of government to provide more money for education in the area "]
el$name_educ[el$second_policy == " lobby higher levels of government to provide more money for education in the area "] <- el$name_2[el$second_policy == " lobby higher levels of government to provide more money for education in the area "]
el$name_educ[el$third_policy == " lobby higher levels of government to provide more money for education in the area "] <- el$name_3[el$third_policy == " lobby higher levels of government to provide more money for education in the area "]
el$name_educ[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "] <- el$name_4[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "]
count(el,name_educ)

el$mr_mrs_educ <- NA
el$mr_mrs_educ[el$third_policy == " lobby higher levels of government to provide more money for education in the area "] <- el$mr_mrs_3[el$third_policy == " lobby higher levels of government to provide more money for education in the area "]
el$mr_mrs_educ[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "] <- el$mr_mrs_4[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "]
count(el,mr_mrs_educ) #note that some are N/A because both candidates were male, if offered as part of 1/2nd policies.

#Add covariate for the non-education policy option offered to respondent:
el$non_educ_policy <- NA
el$non_educ_policy[el$first_policy == " lobby higher levels of government to provide more money for education in the area "] <- 
	el$second_policy[el$first_policy == " lobby higher levels of government to provide more money for education in the area "]
el$non_educ_policy[el$second_policy == " lobby higher levels of government to provide more money for education in the area "] <- 
	el$first_policy[el$second_policy == " lobby higher levels of government to provide more money for education in the area "]
el$non_educ_policy[el$third_policy == " lobby higher levels of government to provide more money for education in the area "] <- 
	el$fourth_policy[el$third_policy == " lobby higher levels of government to provide more money for education in the area "]
el$non_educ_policy[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "] <- 
	el$third_policy[el$fourth_policy == " lobby higher levels of government to provide more money for education in the area "]
count(el,non_educ_policy)

#In order to check allocations, run this command:
#View(dplyr::select(el,name_educ:mr_mrs_educ,non_educ_policy,first_policy:fourth_policy,name_1:name_4,mr_mrs_3:mr_mrs_4))

rm(variable,get_dummy_el)




